package com.zbkj.service.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zbkj.common.request.PageParamRequest;
import com.github.pagehelper.PageHelper;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zbkj.common.exception.AppException;

import com.zbkj.common.request.StoreClerkRecordBrandSearchRequest;
import com.zbkj.common.request.StoreClerkRecordSaveRequest;
import com.zbkj.common.request.StoreClerkRecordSearchRequest;
import com.zbkj.common.response.StoreClerkRecordBrandPageDataResponse;
import com.zbkj.common.response.StoreClerkRecordPageDataResponse;
import com.zbkj.common.result.CommonResultCode;
import com.zbkj.common.model.storeclerk.StoreClerkRecord;
import com.zbkj.service.dao.StoreClerkRecordDao;
import com.zbkj.service.service.StoreClerkRecordService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author dazongzi
* @description StoreClerkRecordServiceImpl 接口实现
* @date 2025-06-27
*/
@Service
public class StoreClerkRecordServiceImpl extends ServiceImpl<StoreClerkRecordDao, StoreClerkRecord> implements StoreClerkRecordService {

    @Resource
    private StoreClerkRecordDao dao;

    @Override
    public Boolean create(StoreClerkRecordSaveRequest storeClerkRecordSaveRequest) {
        StoreClerkRecord storeClerkRecord = new StoreClerkRecord();
        BeanUtils.copyProperties(storeClerkRecordSaveRequest, storeClerkRecord, "id");
        boolean save = save(storeClerkRecord);
        if (!save) {
            throw new AppException(CommonResultCode.VALIDATE_FAILED, "添加失败");
        }
        return save;
    }

    /**
     * 分页查询店小二记录表
     *
     * @param request          请求参数
     * @param pageParamRequest 分页类参数
     * @return List<StoreClerkRecord>
     * @author dazongzi
     * @since 2025-06-27
     */
    @Override
    public List<StoreClerkRecordPageDataResponse> getList(StoreClerkRecordSearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

        //带 StoreClerkRecord 类的多条件查询
        LambdaQueryWrapper<StoreClerkRecord> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(StoreClerkRecord::getStoreId, request.getStoreId());
        lambdaQueryWrapper.eq(StoreClerkRecord::getIsDel, 0);
        lambdaQueryWrapper.orderByAsc(StoreClerkRecord::getCreateTime);
        List<StoreClerkRecord> storeClerkRecords = dao.selectList(lambdaQueryWrapper);
        // 添加活动状态
        List<StoreClerkRecordPageDataResponse> storeClerkRecordResponses = new ArrayList<>();

        storeClerkRecords.stream().forEach(x->{
            StoreClerkRecordPageDataResponse storeClerkRecordResponse = new StoreClerkRecordPageDataResponse();
            BeanUtils.copyProperties(x, storeClerkRecordResponse);
            storeClerkRecordResponses.add(storeClerkRecordResponse);
        });
        return storeClerkRecordResponses;
    }
    /**
     * 分页查询店小二记录表
     *
     * @param request          请求参数
     * @param pageParamRequest 分页类参数
     * @return List<StoreClerkRecord>
     * @author dazongzi
     * @since 2025-06-27
     */
    @Override
    public List<StoreClerkRecordBrandPageDataResponse> getListBrand(StoreClerkRecordBrandSearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

        //带 StoreClerkRecord 类的多条件查询
        LambdaQueryWrapper<StoreClerkRecord> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(StoreClerkRecord::getStoreId, request.getStoreId());
        lambdaQueryWrapper.eq(StoreClerkRecord::getIsDel, 0);
        lambdaQueryWrapper.orderByAsc(StoreClerkRecord::getCreateTime);
        List<StoreClerkRecord> storeClerkRecords = dao.selectList(lambdaQueryWrapper);
        // 添加活动状态
        List<StoreClerkRecordBrandPageDataResponse> storeClerkRecordResponses = new ArrayList<>();

        storeClerkRecords.stream().forEach(x->{
            StoreClerkRecordBrandPageDataResponse storeClerkRecordResponse = new StoreClerkRecordBrandPageDataResponse();
            BeanUtils.copyProperties(x, storeClerkRecordResponse);
            storeClerkRecordResponses.add(storeClerkRecordResponse);
        });
        return storeClerkRecordResponses;
    }

    @Override
    public boolean batchDelete(List<Integer> idList) {
        LambdaUpdateWrapper<StoreClerkRecord> luw = Wrappers.lambdaUpdate();
        luw.set(StoreClerkRecord::getIsDel, true);
        luw.in(StoreClerkRecord::getId, idList);
        boolean update = update(luw);
        if (!update) {
            throw new AppException(CommonResultCode.ERROR.setMessage("批量删除店小二分配记录失败"));
        }
        return false;
    }

    @Override
    public boolean batchDeleteBrand(List<Integer> idList) {
        LambdaUpdateWrapper<StoreClerkRecord> luw = Wrappers.lambdaUpdate();
        luw.set(StoreClerkRecord::getIsDel, true);
        luw.in(StoreClerkRecord::getId, idList);
        boolean update = update(luw);
        if (!update) {
            throw new AppException(CommonResultCode.ERROR.setMessage("批量删除店小二分配记录失败"));
        }
        return false;
    }

    @Override
    public boolean existsRecord(Integer brandId, Integer storeClerkId) {
        LambdaQueryWrapper<StoreClerkRecord> checkWrapper = new LambdaQueryWrapper<>();
        checkWrapper.eq(StoreClerkRecord::getBrandId, brandId);
        checkWrapper.eq(StoreClerkRecord::getStoreId, storeClerkId);
        checkWrapper.eq(StoreClerkRecord::getIsDel, false);
        checkWrapper.eq(StoreClerkRecord::getType, 1);
        long count = count(checkWrapper);
        return count > 0;
    }

    @Override
    public boolean existsRecordBrand(Integer merchantId, Integer storeClerkId) {
        LambdaQueryWrapper<StoreClerkRecord> checkWrapper = new LambdaQueryWrapper<>();
        checkWrapper.eq(StoreClerkRecord::getMerchantId, merchantId);
        checkWrapper.eq(StoreClerkRecord::getStoreId, storeClerkId);
        checkWrapper.eq(StoreClerkRecord::getIsDel, false);
        checkWrapper.eq(StoreClerkRecord::getType, 2);
        long count = count(checkWrapper);
        return count > 0;
    }

    @Override
    public List<StoreClerkRecord> getIdNameList(Integer storeId) {
        LambdaQueryWrapper<StoreClerkRecord> checkWrapper = new LambdaQueryWrapper<>();
        checkWrapper.select(StoreClerkRecord::getId, StoreClerkRecord::getMerchantId);
        checkWrapper.eq(StoreClerkRecord::getIsDel, false);
        checkWrapper.eq(StoreClerkRecord::getStoreId, storeId);
        return list(checkWrapper);
    }

    @Override
    public List<StoreClerkRecord> getListByBrandId(Integer brandId) {
        LambdaQueryWrapper<StoreClerkRecord> checkWrapper = new LambdaQueryWrapper<>();
        checkWrapper.eq(StoreClerkRecord::getIsDel, false);
        checkWrapper.eq(StoreClerkRecord::getBrandId, brandId);
        checkWrapper.eq(StoreClerkRecord::getType, 1);
        return list(checkWrapper);
    }

    @Override
    public List<Integer> getList(Integer brandId) {
        LambdaQueryWrapper<StoreClerkRecord> checkWrapper = new LambdaQueryWrapper<>();
        checkWrapper.select(StoreClerkRecord::getMerchantId);
        checkWrapper.eq(StoreClerkRecord::getIsDel, false);
        checkWrapper.eq(StoreClerkRecord::getBrandId, brandId);
        checkWrapper.eq(StoreClerkRecord::getType, 1);
        List<StoreClerkRecord> list = list(checkWrapper);
        if (CollUtil.isEmpty(list)) {
            return list.stream().map(StoreClerkRecord::getMerchantId).distinct().collect(Collectors.toList());
        }
        return null;
    }

}

